package com.sky.financelake.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.sky.financelake.R;
import com.sky.financelake.bean.dto.SimpleStockZjlxDto;

import java.util.List;

public class SimpleStockZjlxAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final String TAG = SimpleStockZjlxAdapter.class.getCanonicalName();

    private final List<SimpleStockZjlxDto> stockDtos;
    private final Context mContext;

    public SimpleStockZjlxAdapter(List<SimpleStockZjlxDto> stockDtos, Context mContext) {
        this.stockDtos = stockDtos;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_simple_zjlx, parent, false);
        return new StockZjlxViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof StockZjlxViewHolder) {
            ((StockZjlxViewHolder) holder).name.setText(stockDtos.get(position).getName());
            ((StockZjlxViewHolder) holder).date.setText(stockDtos.get(position).getTradeDate());
            Float zjlx = stockDtos.get(position).getZhuliNetInflow();

            float v = stockDtos.get(position).getZhuliNetInflow() / 100000000f;
            float v_ = (float) (Math.round(v * 100)) / 100;
            if (v_ > 0.0f) {
                ((StockZjlxViewHolder) holder).zjlx.setTextColor(Color.RED);
                ((StockZjlxViewHolder) holder).zjlx.setText("+" + v_ + "亿");
            } else if (v_ < 0.0f) {
                ((StockZjlxViewHolder) holder).zjlx.setTextColor(Color.GREEN);
                ((StockZjlxViewHolder) holder).zjlx.setText(v_ + "亿");
            } else {
                ((StockZjlxViewHolder) holder).zjlx.setTextColor(Color.BLACK);
                ((StockZjlxViewHolder) holder).zjlx.setText(mContext.getString(R.string.blank_space) + v_ + "亿");
            }
        }
    }

    @Override
    public int getItemCount() {
        return stockDtos.size();
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }

    public OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.listener = onItemClickListener;
    }

    public class StockZjlxViewHolder extends RecyclerView.ViewHolder {

        AppCompatTextView name, date, zjlx;

        public StockZjlxViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.tv_name);
            date = itemView.findViewById(R.id.tv_date);
            zjlx = itemView.findViewById(R.id.tv_zjlx);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClick(getAdapterPosition());
                    }
                }
            });
        }
    }
}
